package com.qf.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.*;

@Data
@Accessors(chain = true)
@Document(indexName = "hotal_index", shards = 5, replicas = 1)
public class Hotal implements Serializable {

  @TableId(type = IdType.AUTO)
  @Field(type = FieldType.Keyword)
  private Integer id;


  @MultiField(
          mainField =  @Field(type = FieldType.Text, analyzer = "ik_max_word"),
          otherFields = {
                  @InnerField(type = FieldType.Text, analyzer = "pinyin", suffix = "pinyin"),
                  @InnerField(type = FieldType.Keyword, suffix = "keyword")
          }
  )
  private String hotalName;

  @Field(type = FieldType.Keyword, index = false)
  private String hotalImage;
  @Field(type = FieldType.Integer)
  private Integer type;
  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String hotalInfo;
  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String keyword;
  @Transient
  private double lon;
  @Transient
  private double lat;

  //经纬度坐标点
  @GeoPointField
  @TableField(exist = false)
  private double[] location = new double[2];

  @Field(type = FieldType.Integer)
  private Integer star;
  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String brand;
  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String address;
  @DateTimeFormat(pattern = "yyyy-MM-dd")
//  @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
  @Field(type = FieldType.Long)
  private Date openTime;
  @Field(type = FieldType.Integer)
  private Integer cid;
  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String regstr;

  /**
   * 客房的嵌套类型 - 必须初始化，同步到ES中时，ES就会当前数组处理，如果为null，ES就会默认当对象处理
   */
  @Field(type = FieldType.Nested)
  @TableField(exist = false)
  private List<Room> rooms = new ArrayList<>();

  /**
   * 城市对象的嵌套类型
   */
  @Field(type = FieldType.Nested)
  @TableField(exist = false)
  private City city = new City();

  /**
   * 平均价格
   */
  @ScriptedField
  @TableField(exist = false)
  private double avgPrice;

  @ScriptedField
  @TableField(exist = false)
  private double distance;

  /**
   * 点击率
   */
  @Field(type = FieldType.Integer)
  @TableField(exist = false)
  private Integer clicks = 0;

  public void setLon(double lon) {
    this.lon = lon;
    this.location[0] = lon;
  }

  public void setLat(double lat) {
    this.lat = lat;
    this.location[1] = lat;
  }
}
